import request from '@/utils/request'

export function getMenuList(params) {
  return request({
    url: '/dp-server/authority-server/menu/list',
    method: 'get',
    params: params,
    // 添加响应拦截器配置
    transformResponse: [data => {
      // 添加数据类型转换逻辑
      if (Array.isArray(data)) {
        return data.map(menu => ({
          ...menu,
          // 确保permissions字段始终是数组类型
          permissions: Array.isArray(menu.permissions) ? menu.permissions : [],
          // 确保children字段始终是数组类型
          children: Array.isArray(menu.children) ? menu.children : []
        }))
      }
      return data;
    }]
  })
}

export function getRouters() {
  return request({
    url: '/dp-server/authority-server/menu/routers',
    method: 'get'
  })
}

export function getCurrentUserPermissions() {
  return request({
    url: '/dp-server/authority-server/permission/current-user',
    method: 'get'
  })
}

// 添加菜单路由
export const addScopeRoute = (data) => {
  return request({
    url: '/addScopeRoute',
    method: 'post',
    data: data
  })
}

// 删除菜单路由
export const deleteScopeRoute = (data) => {
  return request({
    url: '/deleteScopeRoute',
    method: 'post',
    data: data
  })
}
